package com.hfut.eeg.common.util;

import com.hfut.eeg.dao.ReportMapper;
import com.hfut.eeg.dto.ReportInfo;
import com.hfut.eeg.dto.ReportTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.common.util
 * @Description: TODO
 * @date 2019-11-28 16:19
 */
@Component
public class GetReportTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(GetReportTemplate.class);

    @Autowired
    private ReportMapper reportMapper;

    //驱动表
    private int[] child = {1,3,7,9,12};
    private int[] youngs = {3,5,10,14,55};
    private int[] waves = {0,10,20};

    private String[] properties={"Activity","Stage","AbWave","Seizures","Veeg","Overventilation"};


    private int getChild(int num){
        for (int i=0;i<child.length;i++){
            if(num<=child[i]){
                return i+1;
            }
        }
        return -1;
    }

    private int getYoung(int num){
        for (int i=0;i<youngs.length;i++){
            if(num<=youngs[i]){
                return i+1+child.length;
            }
        }
        return youngs.length+child.length+1;
    }

    private int getWaves(int num){
        for (int i=0;i<waves.length;i++){
            if(num<=waves[i]){
                return i+1;
            }
        }
        return waves.length;
    }
    /**
    * @Description:
    * @param isYear 表示判断传入的age是年单位还是月单位
     * @param age 表示病人年龄，小于一岁
     * @param waveNum 表示异常波数量
     * @param info 表示之前的病人基础信息
    * @return ReportInfo 处理后的报告表信息
    * @author 梁琦
    * @date 2019-11-28 16:20
    * @throws
    */
    public ReportInfo getReportTem(boolean isYear,int age,int waveNum,ReportInfo info){
        ReportTemplate template;
        HashMap<String,Integer> map = new HashMap<>();
        //病人小于一睡
        if(!isYear){
            map.put("fid",getChild(age));
        }else {
            map.put("fid",getYoung(age));
        }
        map.put("preId",getWaves(waveNum));
        template = reportMapper.getTemplate(map);
        if(template == null){
            LOGGER.error("template为空");
        }
        //利用反射机制设置属性
        for (String property : properties) {
            try {
                String tmp = null;
                if (template != null) {
                    tmp = (String) template.getClass().getMethod("get" + property).invoke(template);
                }
                info.getClass().getMethod("set" + property, String.class).invoke(info, tmp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return info;
    }

}
